﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using EnvDTE;
using NielsHebling.UnitTestResultViewerForSL.Core.Data;

namespace NielsHebling.UnitTestResultViewerForSL.Core.Logic
{
    public class SessionInformation : INotifyPropertyChanged
    {        
        #region ProjectXapItems
        private List<ProjectXapItem> _projectXapItems;
        public List<ProjectXapItem> ProjectXapItems 
        {
            get
            {
                return _projectXapItems;                     
            }

            set
            {
                if (value != null)
                {
                    if (value is List<ProjectXapItem>)
                    {
                        _projectXapItems = value;
                        NotifyPropertyChanged("ProjectXapItems");
                    }
                    else
                    {
                        throw new ArgumentException("Type " + value.GetType().ToString() + "is invalid. List<ProjectXapItem> expected.");
                    }
                }
            }
        }
        #endregion

        #region SelectedProjectXapItem
        public ProjectXapItem SelectedProjectXapItem { get; set; }
        #endregion

        #region RunOnBuildDone
        private bool _runOnBuildDone = false;            
        public bool RunOnBuildDone 
        {
            get
            { return _runOnBuildDone; }
            set
            {
                _runOnBuildDone = value;
                NotifyPropertyChanged("RunOnBuildDone");
            }
        }
        #endregion

        private bool _isRunning;
        public bool IsRunning
        {
            get 
            {
                return _isRunning;
                     
            }
            set 
            {
                _isRunning = value;
                NotifyPropertyChanged("IsRunning");    
            }
        }

        public List<string> XmlReportList { get; set; }

        #region TestResults
        private List<Result> _testResults;
        public List<Result> TestResults 
        {
            get
            {
                return _testResults;
            }
            set
            {
                if (value != null)
                {
                    _testResults = value;
                    NotifyPropertyChanged("TestResults");
                }
            }
        }
        #endregion

        private ViewResultDataList _combinedTestResult;
        public ViewResultDataList CombinedTestResult
        {
            get
            {
                return _combinedTestResult;
            }
            set
            {
                if (value != null)
                {
                    _combinedTestResult = value;
                    NotifyPropertyChanged("CombinedTestResult");
                }
            }
        }

        #region ctor
        public SessionInformation()
        {
            _projectXapItems = new List<ProjectXapItem>();            
        }
        #endregion

        public List<string> VerifyXapItems()
        {            
            List<string> result = new List<string>();

            if (this.SelectedProjectXapItem.XapFilePath == "all")
            {
                if (MessageBox.Show("This is currently unstable.\nIE might not close automatically.", "Unstable", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK)
                {
                    foreach (ProjectXapItem xap in this.ProjectXapItems)
                    {
                        FileInfo fi = new FileInfo(xap.XapFilePath);
                        if (fi.Exists)
                        {
                            result.Add(xap.XapFilePath);
                        }
                    }
                }
            }
            else
            {                
                List<Project> allProjects = VsShellAbstractionLayer.Instance.GetFilesFromProjects();
                Project project = allProjects.SingleOrDefault(x => x.Name == this.SelectedProjectXapItem.ProjectName);                

                Properties activeProjectConfigurationProperties = project.ConfigurationManager.ActiveConfiguration.Properties;

                object outputPath = activeProjectConfigurationProperties.Item("OutputPath").Value;
                if (outputPath == null)
                {
                    throw new Exception("OutputPath project property not found");
                }

                string outputFilePath = Path.Combine(
                    Path.GetDirectoryName(project.FullName),
                    outputPath.ToString(),
                    this.SelectedProjectXapItem.XapFilePath);

                VsShellAbstractionLayer.Instance.Trace(outputFilePath);
                FileInfo fi = new FileInfo(outputFilePath);
                if (fi.Exists)
                {
                    result.Add(outputFilePath);
                }
            }
            return result;
        }       

        public void ClearResults()
        {
            if(XmlReportList != null)
            {
                XmlReportList.Clear();
            }
            else
            {
                XmlReportList = new List<string>();
            }

            if (TestResults != null)
            {
                TestResults.Clear();
            }
            else
            {
                TestResults = new List<Result>();
            }

            if (CombinedTestResult != null)
            {
                if (CombinedTestResult.Items != null)
                {
                    CombinedTestResult.Items.Clear();
                }
                CombinedTestResult = null;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion
    }
}
